package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Author  zxy
 * Date  2022/11/16 下午 2:53
 */
@Controller
public class TestScopeController {

    @RequestMapping("/test/mav")
    public ModelAndView testMAV(){
        /*
        * model: 向请求域中共享数据
        * view: 设置逻辑视图实现页面跳转
        * */
        ModelAndView mav = new ModelAndView();
        // 共享数据
        mav.addObject("testRequestScope", "hello, ModelAndView");
        // 设置逻辑视图
        mav.setViewName("success");
        // 一定要返回ModelAndView
        return mav;
    }

    @RequestMapping("/test/model")
    public String testMode(Model model) {
        model.addAttribute("testRequestScope", "hello, Model");
        return "success";
    }

    @RequestMapping("/test/modelMap")
    public String testModelMap(ModelMap modelMap) {
        modelMap.addAttribute("testRequestScope", "hello, ModelMap");
        return "success";
    }

    @RequestMapping("/test/map")
    public String testMap(Map<String, Object> map) {
        map.put("testRequestScope", "hello, map");
        return "success";
    }

    /*
    * request: 一次请求
    * session: 一次浏览器关闭
    * application：一次服务器关闭
    * */
    @RequestMapping("/test/session")
    public String testSession(HttpSession session) {
        session.setAttribute("testSessionScope", "hello, session");
        return "success";
    }

    @RequestMapping("/test/application")
    public String testapplication(HttpSession session) {
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute("testApplicationSCope", "hello, application");
        return "success";
    }

}
